package com.studyweb.service.impl;

import com.studyweb.mapper.VocabularyMapper;
import com.studyweb.pojo.entity.Vocabulary;
import com.studyweb.service.VocabularyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 词语积累服务实现类
 */
@Service
@Slf4j
public class VocabularyServiceImpl implements VocabularyService {

    @Autowired
    private VocabularyMapper vocabularyMapper;

    @Override
    @Transactional
    public boolean addVocabulary(Vocabulary vocabulary) {
        LocalDateTime now = LocalDateTime.now();
        vocabulary.setCreateTime(now);
        vocabulary.setUpdateTime(now);
        
        return vocabularyMapper.insert(vocabulary) > 0;
    }

    @Override
    public Vocabulary getVocabularyById(Long id) {
        return vocabularyMapper.selectById(id);
    }

    @Override
    @Transactional
    public boolean updateVocabulary(Vocabulary vocabulary) {
        vocabulary.setUpdateTime(LocalDateTime.now());
        return vocabularyMapper.update(vocabulary) > 0;
    }

    @Override
    @Transactional
    public boolean deleteVocabulary(Long id) {
        return vocabularyMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional
    public boolean batchDeleteVocabulary(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return false;
        }
        try {
            int deletedCount = vocabularyMapper.batchDeleteByIds(ids);
            return deletedCount == ids.size();
        } catch (Exception e) {
            log.error("批量删除词语失败：{}", e.getMessage(), e);
            return false;
        }
    }

    @Override
    public Map<String, Object> getVocabularyList(int pageNum, int pageSize, String keyword) {
        // 计算偏移量
        int offset = (pageNum - 1) * pageSize;
        
        // 查询词语列表
        List<Vocabulary> vocabularies = vocabularyMapper.selectVocabularies(offset, pageSize, keyword);
        
        // 查询总数
        int total = vocabularyMapper.countVocabularies(keyword);
        
        // 封装结果
        Map<String, Object> result = new HashMap<>();
        result.put("list", vocabularies);
        result.put("total", total);
        result.put("pageNum", pageNum);
        result.put("pageSize", pageSize);
        
        return result;
    }

    @Override
    public List<Vocabulary> getPracticeVocabularies(int count) {
        return vocabularyMapper.selectPracticeVocabularies(count);
    }
    
    @Override
    public List<Vocabulary> getAllVocabularies() {
        return vocabularyMapper.selectAllVocabularies();
    }

    @Override
    @Transactional
    public boolean updateMasteryLevel(Long id, Integer masteryLevel, Integer reviewCount) {
        return vocabularyMapper.updateMasteryLevel(id, masteryLevel, reviewCount) > 0;
    }
}